﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Web.Admin.Services.Sites;
using SaaS.Model.Entity.Sites;

namespace Web.Admin.Controllers.Sites
{
    [Route("api/[controller]")]
    [ApiController]
    public class RoleController : AdminControllerBase
    {
        private readonly RoleAppService _roleAppService;
        public RoleController(RoleAppService roleAppService)
        {
            _roleAppService = roleAppService;
        }
        /// <summary>
        /// 列表
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ActionResult List()
        {
            IQueryable<SiteRole> query = ADC.SiteRoles.Where(c => c.SiteId == Session.SiteId);
            return PageResult(query.OrderByDescending(c => c.Id), c => new
            {
                c.Id,
                c.Name,
                c.Description,
                c.Count
            });
        }
        /// <summary>
        /// 保存
        /// </summary>
        /// <param name="id"></param>
        /// <param name="name"></param>
        /// <param name="description"></param>
        /// <param name="permissions"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Save(int? id, string name, string description, string[] permissions)
        {
            return Ok(_roleAppService.CreateOrEdit(id ?? 0, name, description, permissions));
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpDelete]
        public ActionResult Delete(int id)
        {
            return Ok(_roleAppService.Delete(id));
        }
        /// <summary>
        /// 信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public ActionResult Info(int id)
        {
            var role = ADC.SiteRoles.Where(c => c.SiteId == Session.SiteId && c.Id == id).FirstOrDefault();
            return Ok(new
            {
                role.Id,
                role.Name,
                role.Permissions,
                role.Description
            });
        }


    }
}